<?php
/**
 * Created by PhpStorm.
 * User: lgm
 * Date: 2018/9/3
 * Time: 18:14
 */
namespace app\admin\controller;

use think\Db;
use think\facade\Request;
use think\Image;

class Info extends MyController
{
    //资讯列表
    public function index()
    {
        $cid = input('cid','1');
        //资讯分类
        $infocates = Db::table('ts_info_category')->select();


        $name =db('info_category')->where('cat_id',$cid)->value('cat_title');
        $current = input('page','1');
        $length = 8;
        $start = ($current -1)*$length;

        $info = db('information')
            ->where('cat_id',$cid)
            ->order('info_id','DESC')
            ->limit($start,$length)
            ->paginate($length);

        $total = $info->total();


        $this->assign([
            'infocates' => $infocates,
            'cid'=>$cid,
            'name' =>$name,
            'info'=> $info,
            'total' =>$total
        ]);
        return view();
    }
    //添加资讯
    public function add($cid)
    {
        //添加动作
        if ( Request::isPost()){
            $data = Request::post();
            $data['pubtime']  = time();

            //上传图片
            $data['img'] = uploadimg('image','../public/static/uploads');

            $row = Db::name('information')
                ->data($data)
                ->insert();
            if ($row==1){
                $this->success('添加成功！','/admin/info/index/cid/'.$cid);
            }
        }
        $infocates = Db::table('ts_info_category')->select();
        $name =db('info_category')->where('cat_id',$cid)->value('cat_title');
        $this->assign([
            'cid'=>$cid,
            'name'=>$name,
            'infocates'=>$infocates
        ]);
        return view();
    }
    //编辑资讯
    public function edit($iid)
    {
        $iid = input('iid');
        $cid =db('information')->where('info_id',$iid)->value('cat_id');
        //修改动作
        if ( Request::isPost()){
            $data = Request::post();
            //判断是否上传图片
            if (!empty(request()->file('image'))){
                $data['img'] = uploadimg('image','../public/static/uploads');
            }

            $row = Db::name('information')
                ->data($data)
                ->where('info_id',$iid)
                ->update();
            if ($row==1){
                $this->success('修改成功','/admin/info/index/cid/'.$cid);
            }
        }
        $name =db('info_category')->where('cat_id',$cid)->value('cat_title');
        $info = db('information')->where('info_id',$iid)->find();
        $infocates = Db::table('ts_info_category')->select();
        $this->assign([
            'cid'=>$cid,
            'name'=>$name,
            'info' =>$info,
            'infocates'=>$infocates
        ]);
        return view();
    }
    //删除资讯
    public function del()
    {
        $iid  =  input('iid');
        $del = db('information')->field('img,cat_id')->where('info_id',$iid)->find();
        $flag = Db::table('ts_information')->where('info_id',$iid)->delete();

        if ($flag){
            //删除成功
            if (!empty($del['img']))unlink(config('upload.path').$del['img']);//删除图片
            $this->success('删除成功','/admin/info/index/cid/'.$del['cat_id']);
        }else{
            $this->error('删除失败！');
        }
    }
}
